package com.joysuch.wwyt.workflow.service.impl;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.util.PageableUtil;
import com.joysuch.wwyt.workflow.bean.WorkFlowOptionBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowOptionTypeBean;
import com.joysuch.wwyt.workflow.entity.WorkFlowOption;
import com.joysuch.wwyt.workflow.entity.WorkFlowOptionType;
import com.joysuch.wwyt.workflow.repository.WorkFlowOptionDao;
import com.joysuch.wwyt.workflow.repository.WorkFlowOptionTypeDao;
import com.joysuch.wwyt.workflow.service.WorkFlowOptionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WorkFlowOptionServiceImpl implements WorkFlowOptionService {

    @Autowired
    private WorkFlowOptionDao workFlowOptionDao;

    @Autowired
    private WorkFlowOptionTypeDao workFlowOptionTypeDao;

    @Override
    public List<WorkFlowOptionType> getOptionTypeList() {
        return workFlowOptionTypeDao.findAll();
    }

    @Override
    public List<WorkFlowOption> getOptionList(Long typeId) {
        return workFlowOptionDao.findAllByOptionTypeOrderByShowOrder(typeId);
    }

    @Override
    public List<WorkFlowOption> getOptionList(String typeName) {
        return workFlowOptionDao.findAllByOptionTypeName(typeName);
    }

    @Override
    public WorkFlowOptionType saveOptionType(WorkFlowOptionTypeBean bean) {
        if (StringUtils.isBlank(bean.getOptionType())) {
            throw new IllegalArgumentException("名称不能为空");
        }
        WorkFlowOptionType optionType = null;
        if (bean.getId() == null || bean.getId() == 0) {
            optionType = new WorkFlowOptionType();
        } else {
            optionType = workFlowOptionTypeDao.findById(bean.getId()).get();
        }
        optionType.setOptionType(bean.getOptionType());
        optionType = workFlowOptionTypeDao.save(optionType);
        int count = workFlowOptionTypeDao.countByOptionType(bean.getOptionType());
        if (count > 1) {
            throw new IllegalArgumentException("名称已存在");
        }
        return optionType;
    }


    @Override
    public WorkFlowOption saveOption(WorkFlowOptionBean bean) {
        WorkFlowOption workFlowOption = null;
        if (bean.getId() == null || bean.getId() == 0) {
            workFlowOption = new WorkFlowOption();
        } else {
            workFlowOption = workFlowOptionDao.findById(bean.getId()).get();
        }

        workFlowOption.setOptionName(bean.getOptionName());
        workFlowOption.setOptionType(bean.getOptionType());
        workFlowOption.setShowOrder(bean.getShowOrder());
        workFlowOption = workFlowOptionDao.save(workFlowOption);
        int count = workFlowOptionDao.countByOptionTypeAndOptionName(workFlowOption.getOptionType(), workFlowOption.getOptionName());
        if (count > 1) {
            throw new IllegalArgumentException("选项名称在该类型下已存在");
        }
        return workFlowOptionDao.save(workFlowOption);
    }

    @Override
    public WorkFlowOptionType getOptionType(Long id) {
        return workFlowOptionTypeDao.findById(id).get();
    }

    @Override
    public WorkFlowOption getOption(Long id) {
        return workFlowOptionDao.findById(id).get();
    }

    @Override
    public void deleteOptionType(Long id) {
        workFlowOptionTypeDao.deleteById(id);
    }

    @Override
    public void deleteOption(Long id) {
        workFlowOptionDao.deleteById(id);
    }

    /**
     * 新增选项组件
     *
     * @param workFlowOptionBeans
     * @param optionType
     * @return
     */
    @Override
    public ResultBean addOptions(List<WorkFlowOptionBean> workFlowOptionBeans, Long optionType) {
        List<WorkFlowOption> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(workFlowOptionBeans)) {
            //根据选项名称去重
            workFlowOptionBeans = workFlowOptionBeans.stream()
                    .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(WorkFlowOptionBean::getOptionName))), ArrayList::new));
            for (WorkFlowOptionBean bean : workFlowOptionBeans) {
                WorkFlowOption option = new WorkFlowOption();
                bean.setOptionType(optionType);
                int count = workFlowOptionDao.countByOptionTypeAndOptionName(bean.getOptionType(), bean.getOptionName());
                if (count > 0) {
                    throw new IllegalArgumentException(bean.getOptionName() + "已存在");
                }
                BeanUtils.copyProperties(bean, option);
                list.add(option);
            }
            if (!CollectionUtils.isEmpty(list)) {
                workFlowOptionDao.saveAll(list);
            }
        }
        return ResultBean.success("");
    }

    /**
     * 分页选项类型
     *
     * @param page
     * @param optionType
     * @return
     */
    @Override
    public ResultBean pageOptionType(Pageable page, String optionType) {
        List<WorkFlowOptionType> list = new ArrayList<>();
        list = workFlowOptionTypeDao.findByOptionTypeLike(optionType);
        if (!CollectionUtils.isEmpty(list)) {
            List<WorkFlowOptionType> wfList = PageableUtil.getList(page, list);
            return ResultBean.pageData(wfList, list.size());
        }
        return ResultBean.success(list);
    }
}
